package mac10.monitor;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Collection;
import java.util.Set;

import javax.swing.JComponent;

import mac10.models.Agent;
import mac10.models.Cell;
import mac10.models.Cluster;
import mac10.models.ClusterBook;
import mac10.models.WorldModel;

public class CellComponent extends JComponent
{

	private static final long serialVersionUID = -9096490769156187156L;
	
	public final static Color UNKNOWN = new Color(40, 40, 40, 255);
    public final static Color EMPTY = new Color(190, 190, 190, 255);
    public final static Color ALLY_AGENT = new Color(0, 0, 255, 255);
    public final static Color ENEMY_AGENT = new Color(255, 0, 0, 255);
    public final static Color ALLY_CORRAL = new Color(0, 250, 154, 255);
    public final static Color ENEMY_CORRAL = new Color(255, 114, 86, 255);
    public final static Color OBSTACLE = new Color(94, 170, 74, 255);
    public final static Color OPEN_FENCE = new Color(255, 255, 0, 100);
    public final static Color CLOSED_FENCE = new Color(255, 255, 0, 255);
    public final static Color SWITCH = new Color(153, 50, 204, 255);
    public final static Color COW = new Color(255, 255, 255, 255);

    public final static Color EMPTY_DARK = new Color(150, 150, 150, 255);
    public final static Color ENEMY_AGENT_DARK = new Color(215, 0, 0, 255);
    public final static Color ALLY_CORRAL_DARK = new Color(0, 210, 114, 255);
    public final static Color ENEMY_CORRAL_DARK = new Color(215, 74, 46, 255);
    public final static Color OBSTACLE_DARK = new Color(54, 130, 34, 255);
    public final static Color OPEN_FENCE_DARK = new Color(215, 215, 0, 100);
    public final static Color CLOSED_FENCE_DARK = new Color(215, 215, 0, 255);
    public final static Color SWITCH_DARK = new Color(113, 10, 164, 255);
    public final static Color COW_DARK = new Color(215, 215, 215, 255);

    public static boolean drawGrid = false;
    public static boolean drawSimple = true;
    public static boolean drawEdgeDetection = true; 
    
	private Color cellColor;
	private BufferedImage cellImage;
	private Cell cell;
	
	public CellComponent(Cell cell)
	{
	    this(cell, UNKNOWN, null);
	}
	
	public CellComponent(Cell cell, Color color, BufferedImage image)
	{
	    this.cell = cell;
	    this.cellColor = color;
	    this.cellImage = image;
	    this.setMinimumSize(new Dimension(5, 5));
	    this.setToolTipText("("+cell.getX()+", "+cell.getY()+")");
//	    this.addMouseListener(new MouseListener()
//        {
//            public void mouseReleased (MouseEvent e)
//            {
//                // TODO Auto-generated method stub
//                
//            }
//            
//            public void mousePressed (MouseEvent e)
//            {
//                // TODO Auto-generated method stub
//                
//            }
//            
//            public void mouseExited (MouseEvent e)
//            {
//                // TODO Auto-generated method stub
//                
//            }
//            
//            public void mouseEntered (MouseEvent e)
//            {
//                // TODO Auto-generated method stub
//                
//            }
//            
//            public void mouseClicked (MouseEvent e)
//            {
//                // TODO Auto-generated method stub
//                
//            }
//        });
	}
	
	public void paintComponent(Graphics g)
	{
		if (cell.isAllyAgent()) {
			Set<String> keys =WorldModel.staticWorldModel.agents.keySet();
			for (String key:keys) {
				Agent agent = WorldModel.staticWorldModel.agents.get(key);
				if (agent.x == cell.getX() && agent.y == cell.getY()) {
					setToolTipText(agent.name);
					break;
				}
			}
		} else if (cell.isCow()) {
			Collection<Cluster> book = ClusterBook.staticClusterBook.getBook().values();
			for (Cluster x:book) {
				for (Cell c:x.getList()) {
					if (c.equals(cell)) {
						setToolTipText(x.getId());
						break;
					}
				}
			}
		}
		if (drawEdgeDetection) {
			if (cell.isAllyCorral()) {
				g.setColor(Color.BLUE);
				g.fillRect(0, 0, getWidth(), getHeight());
			} else if (cell.isObstacle()) {
				g.setColor(Color.GREEN);
				g.fillRect(0, 0, getWidth(), getHeight());
			} else {
				int c = cell.edgeValue * 28;
				g.setColor(new Color(c, c, c));
				g.fillRect(0, 0, getWidth(), getHeight());

//				if (!WorldModel.staticWorldModel.getHerds().isEmpty()) {
//					if (WorldModel.staticWorldModel.getHerds().contains(cell)) {
//						g.setColor(Color.RED);
//		                g.drawLine(0, getHeight()/2, getWidth()-1, getHeight()/2);
//		                g.drawLine(getWidth()/2, getHeight()-1, getWidth()/2, 0);
//					}
//				}
			}
		}
		
		else if (drawSimple)
	    {
	        if (drawGrid)
	        {
        	    g.setColor(cellColor);
                g.fillRect(0, 0, getWidth() - 1, getHeight() - 1);
            }
            else if (cell.getClusterColor() != null)
            {
                g.setColor(cell.getClusterColor());
                g.fillRect(0, 0, getWidth(), getHeight());
            }
	        else
	        {
	            g.setColor(cellColor);
                g.fillRect(0, 0, getWidth(), getHeight());
	        }
	        
	        if( cell.getMarked() == Cell.RED_CROSS )
	        {
	            g.setColor(Color.RED);
	            g.drawLine(0, 0, getWidth()-1, getHeight()-1);
                g.drawLine(0, getHeight()-1, getWidth()-1, 0);
	        }
	        else if( cell.getMarked() == Cell.BLACK_CROSS )
            {
                g.setColor(Color.BLACK);
                g.drawLine(0, getHeight()/2, getWidth()-1, getHeight()/2);
                g.drawLine(getWidth()/2, getHeight()-1, getWidth()/2, 0);
            }
	        else if( cell.getMarked() == Cell.MAGENTA_CIRCLE )
            {
                g.setColor(Color.MAGENTA);
                g.drawOval(0, 0, getWidth()-1, getHeight()-1);
            }
	        else if( cell.getMarked() == Cell.BLUE_CIRCLE )
            {
                g.setColor(Color.BLUE);
                g.drawOval(0, 0, getWidth()-1, getHeight()-1);
            }

	    }
    	else if ( cellImage != null )
    	{
    	    if (drawGrid)
            { 
    	        g.drawImage(cellImage, 0, 0, getWidth()-1, getHeight()-1, this);
            }
    	    else
    	    {
    	        g.drawImage(cellImage, 0, 0, getWidth(), getHeight(), this);
    	    }
    	}
	}

    /**
     * @return the cellColor
     */
    public Color getCellColor ()
    {
        return cellColor;
    }

    /**
     * @param cellColor the cellColor to set
     */
    public void setCellColor (Color cellColor)
    {
        this.cellColor = cellColor;
    }

    /**
     * @return the cellImage
     */
    public BufferedImage getCellImage ()
    {
        return cellImage;
    }

    /**
     * @param cellImage the cellImage to set
     */
    public void setCellImage (BufferedImage cellImage)
    {
        this.cellImage = cellImage;
    }

    /**
     * @return the cell
     */
    public Cell getCell ()
    {
        return cell;
    }

    /**
     * @param cell the cell to set
     */
    public void setCell (Cell cell)
    {
        this.cell = cell;
        this.setToolTipText("("+cell.getX()+", "+cell.getY()+")");
    }
}
